package cc.mrbird.febs.buss.service.impl;

import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.buss.entity.Policy;
import cc.mrbird.febs.buss.mapper.PolicyMapper;
import cc.mrbird.febs.buss.service.IPolicyService;
import cc.mrbird.febs.system.entity.Dept;
import cc.mrbird.febs.system.mapper.DeptMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.beans.factory.annotation.Autowired;
import cc.mrbird.febs.common.utils.SortUtil;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
/**
 *  Service实现
 *
 * @author Lujingyuan
 * @date 2020-11-26 10:26:58
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class PolicyServiceImpl extends ServiceImpl<PolicyMapper, Policy> implements IPolicyService {

    /*@Autowired
    private PolicyMapper policyMapper;*/
    @Autowired
    private DeptMapper deptMapper;


    @Override
    public IPage<Policy> findPolicyDetail(QueryRequest request, HashMap<String,String> map) {
        Page<Policy> page = new Page<Policy>(request.getPageNum(), request.getPageSize());
        SortUtil.handlePageSort(request, page, "id", FebsConstant.ORDER_ASC, false);
        IPage<Policy> pageByMap = this.baseMapper.findPolicyDetailPageByMap(page, map);
        List<Policy> policyList = pageByMap.getRecords();
        for (Policy policy : policyList) {
            Dept dept = deptMapper.selectById(policy.getDeptId());
            policy.setDeptName(dept.getDeptName());
        }
        return pageByMap;
    }

    @Override
    public List<Policy> findPolicys(Policy policy) {
	    LambdaQueryWrapper<Policy> queryWrapper = new LambdaQueryWrapper<>();
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional
    public void createPolicy(Policy policy) {
        policy.setCreatedTime(new Date());
        this.save(policy);
    }

    @Override
    @Transactional
    public void updatePolicy(Policy policy) {
        policy.setUpdatedTime(new Date());
        this.saveOrUpdate(policy);
    }

    @Override
    @Transactional
    public void deletePolicy(Policy policy) {
        LambdaQueryWrapper<Policy> wrapper = new LambdaQueryWrapper<>();
	    // TODO 设置删除条件
	    this.remove(wrapper);
	}

    @Override
    @Transactional
    public void deletePolicy(String[] ids) {
        List<String> list = Arrays.asList(ids);
        // 删除用户
        this.removeByIds(list);
    }

    /**
     * 根据deptId查询政策列表
     * @return
     */
    @Override
    public List<Policy> getListByDeptId(String deptId) {
        QueryWrapper<Policy> wrapper = new QueryWrapper<>();
        wrapper.eq("dept_id",deptId);
        wrapper.orderByDesc("publish_time");
        List<Policy> policyList = this.baseMapper.selectList(wrapper);
        return policyList;
    }
}
